package com.ygo.feihua.view;
import android.widget.*;
import android.content.*;
import android.util.*;
import android.graphics.*;
import com.ygo.feihua.*;
import android.os.*;
import android.view.*;
import android.net.*;
import android.text.util.*;

public class LiaotianMessageLayout extends LinearLayout
{
	private final String messageReferenceStart="--[";
	private final String messageReferenceEnd="]";
	private final String typeCard="kp";
	private final String typeDeck="kz";
	private final String typeZhuti="zt";
	
	private Context context;
	
	public LiaotianMessageLayout(Context context,AttributeSet attrs){
		super(context,attrs);
		this.context=context;
		//设置LinearLayout水平或垂直
        setOrientation(LinearLayout.VERTICAL);
		//LinearLayout背景
        setBackgroundColor(Color.TRANSPARENT);
	}

	public void setMessage(String s){
		removeAllViews();
		int start;
		int end;
		int lastStart=0;
		int lastEnd=0;
		do{

		 start=s.indexOf(messageReferenceStart,lastEnd);
		
			if (start != -1){
				end = s.indexOf(messageReferenceEnd, start);
				if (end == -1){
					if(lastEnd==0){
						showTv(s.substring(0, s.length()));
					}else{
						showTv(s.substring(lastEnd + messageReferenceEnd.length(), s.length()));		
					}
					return;
				}else{
					
					
					switch (s.substring(start + messageReferenceStart.length(), start + messageReferenceStart.length() + 2))
					{
						case typeCard:
							if (start != 0 && start != lastEnd + 1)
							{
								if (lastStart == 0)
								{
									showTv(s.substring(0, start));
								}else{

									showTv(s.substring(lastEnd+messageReferenceEnd.length(),start));
								}
							}
							String ss=s.substring(start + messageReferenceStart.length()+typeCard.length(), end);
							showCard(ss);

							break;
						case typeDeck:
							if (start != 0 && start != lastEnd + 1)
							{
								if (lastStart == 0)
								{
									showTv(s.substring(0, start));
								}else{

									showTv(s.substring(lastEnd+messageReferenceEnd.length(),start));
								}
							}
							String ss1=s.substring(start + messageReferenceStart.length()+typeCard.length(), end);
							showDeck(ss1);
							
							break;
					/*	case typeZhuti:
							if (start != 0 && start != lastEnd + 1)
							{
								if (lastStart == 0)
								{
									showTv(s.substring(0, start));
								}else{

									showTv(s.substring(lastEnd+messageReferenceEnd.length(),start));
								}
							}
							String ss2=s.substring(start + messageReferenceStart.length()+typeCard.length(), end);
							showZhuti(ss2);
							break;*/
						default:
						if(lastEnd==0){
							showTv(s.substring(0,end+messageReferenceEnd.length()));
						}else{
							showTv(s.substring(lastEnd+messageReferenceEnd.length(),end+messageReferenceEnd.length()));
						}
						break;
					}
					lastStart = start;
					lastEnd = end;
				}
			}else{			
				if(lastEnd==0){
					showTv(s.substring(0, s.length()));
				}else{
					showTv(s.substring(lastEnd + messageReferenceEnd.length(), s.length()));		
				}
				return;
			}
		}while(true);
	}
	
	private void showDeck(String s){
		
		
	}
	
	private void showTv(String s){
		TextView tv=new TextView(context);
		tv.setIncludeFontPadding(false);
		tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		tv.setTextSize(16);
		tv.setTextColor(Color.BLACK);
		tv.setBackgroundColor(Color.TRANSPARENT);
		tv.setAutoLinkMask(Linkify.PHONE_NUMBERS |Linkify.WEB_URLS|Linkify.EMAIL_ADDRESSES);
		tv.setText(s);
		addView(tv);
	}
	
	
	private void showCard(final String km){
		final ImageView iv=new ImageView(context);
		LayoutParams lp= new LinearLayout.LayoutParams(dpToPx(80),dpToPx(115));
		lp.setMargins(0,5,5,0);
		iv.setLayoutParams(lp);
		iv.setScaleType(ImageView.ScaleType.FIT_XY);
		iv.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
				/*	Intent i=new Intent(context, A5.class);
					i.putExtra("km", km);
					context.startActivity(i);*/
					// TODO: Implement this method
				}
			});
			Gongju.setKt(context,iv,km);
		
		addView(iv);
	}
	
	public  int dpToPx(int dp){
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int)(dp * density + 0.5f);
    }
	
}
